<?php
/**
 * Created by PhpStorm.
 * User: saive
 * Date: 16-4-28
 * Time: 下午11:03
 */
/**
 * 打印数组
 * @param $arr
 */
function _dump($arr){

    echo '<pre>';
    print_r($arr);
    echo '</pre>';

}

/**
 * 过虑掉父级目录
 * @param $arr
 * @return mixed
 */
function _filter($arr){
    foreach($arr as $key=>$item) {
        if(substr($item,0,1) == ".") {
            unset($arr[$key]);
        }
    }

    return $arr;
}

/**
 * 生成配置文件
 * @param $params
 * @return array
 */
function _buildConfig($params) {
    global $template;
    $search = array(
        '{{alias}}','{{domain}}','{{workspace}}','{{filename}}'
    );

    $params = array(
        'alias' => $_POST['alias'],
        'domain' => $_POST['domain'],
        'workspace' => "/workspace/".$_POST['workspace'],
        'filename' => $_POST['filename'],
    );

    foreach($params as $v) {
        if(!$v) {
            return array('status'=>0,'msg'=>'数据不全！','data'=>null);
        }
    }

    $content = str_replace($search,$params,$template);
    $filename = "/etc/nginx/conf.d/".$params['filename'].'.conf';
    if(file_exists($filename)) {
        return array('status'=>0,'msg'=>'配置已经存在，请确认后再操作！','data'=>null);
    } else {
        file_put_contents($filename,$content);
    }
    return array('status'=>1,'msg'=>'配置成功','data'=>array($content,$filename));
}